﻿using Alipay.AopSdk.F2FPay.Domain;
using Alipay.AopSdk.F2FPay.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThirdParty.Application.Model.ViewModel.Request
{
    /// <summary>
    ///  创建二维码订单请求
    /// </summary>
    public class AlipayCreateQRCodeOrderReqDto
    {
        /// <summary>
        /// 订单编号(非空)
        /// </summary>
        public string OrderId { get; set; }

        /// <summary>
        /// 订单名称(非空)
        /// </summary>
        public string OrderName { get; set; }

        /// <summary>
        /// 支付金额(非空)
        /// </summary>
        public string PayAmount { get; set; }


        /// <summary>
        /// 订单描述
        /// </summary>
        public string OrderDesc { get; set; }

        /// <summary>
        /// 操作人
        /// </summary>
        public string OperatorUser { get; set; }


        /// <summary>
        /// 门店编号
        /// </summary>
        public string StoreId { get; set; }

        /// <summary>
        /// 异步通知地址
        /// </summary>
        public string NotifyUrl { get; set; }

        /// <summary>
        /// 收款账号
        /// </summary>
        public string AliPayPid { get; set; }

        /// <summary>
        /// 商品详情
        /// </summary>
        public List<CommodityDetail> CommodityDetails { get; set; }

        /// <summary>
        /// 校验
        /// </summary>
        /// <returns></returns>
        public bool Validate()
        {
            return !string.IsNullOrWhiteSpace(OrderId) && !string.IsNullOrWhiteSpace(OrderId) && !string.IsNullOrWhiteSpace(OrderId);
        }

        /// <summary>
        /// 获取支付宝商品详情
        /// </summary>
        public static AlipayTradePrecreateContentBuilder GetAlipayTradePrecreateContentBuilder(AlipayCreateQRCodeOrderReqDto alipayQRCodeSubmit)
        {
            if (!alipayQRCodeSubmit.Validate())
                return null;
            return new AlipayTradePrecreateContentBuilder
            {
                //收款账号
                seller_id = alipayQRCodeSubmit.AliPayPid,
                //订单编号
                out_trade_no = alipayQRCodeSubmit.OrderId.Trim(),
                //订单总金额
                total_amount = alipayQRCodeSubmit.PayAmount.Trim(),
                //参与优惠计算的金额
                //builder.discountable_amount = "";
                //不参与优惠计算的金额
                //builder.undiscountable_amount = "";
                //订单名称
                subject = alipayQRCodeSubmit.OrderName.Trim(),
                //自定义超时时间
                timeout_express = "10m",
                //订单描述
                body = alipayQRCodeSubmit.OrderDesc,
                //门店编号，很重要的参数，可以用作之后的营销
                store_id = alipayQRCodeSubmit.StoreId,
                //操作员编号，很重要的参数，可以用作之后的营销
                operator_id = alipayQRCodeSubmit.OperatorUser,
                //订单商品详情
                goods_detail = alipayQRCodeSubmit.CommodityDetails.Select(x => CommodityDetail.GetGoodsInfo(x)).ToList(),
                //系统商接入可以填此参数用作返佣
                //ExtendParams exParam = new ExtendParams();
                //exParam.sysServiceProviderId = "20880000000000";
                //builder.extendParams = exParam;
            };
        }
    }

    /// <summary>
    /// 商品详情
    /// </summary>
    public class CommodityDetail
    {

        /// <summary>
        /// 商品id
        /// </summary>
        public string GoodsId { get; set; }
        /// <summary>
        /// 商品id
        /// </summary>
        public string GoodsName { get; set; }

        /// <summary>
        /// 商品单价
        /// </summary>
        public string GoodsPrice { get; set; }

        /// <summary>
        /// 商品数量
        /// </summary>
        public int GoodsQuantity { get; set; }

        /// <summary>
        /// 获取支付宝商品详情
        /// </summary>
        public static GoodsInfo GetGoodsInfo(CommodityDetail commodityDetail)
        {
            return new GoodsInfo
            {
                goods_id = commodityDetail.GoodsId,
                goods_name = commodityDetail.GoodsName,
                price = commodityDetail.GoodsPrice.ToString(),
                quantity = commodityDetail.GoodsQuantity.ToString()
            };
        }
    }
}
